package com.infoholdcity.common.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infoholdcity.common.common.Result;
import com.infoholdcity.common.constant.CacheConstant;
import com.infoholdcity.common.constant.CommonConstant;
import com.infoholdcity.common.entity.SysUser;
import com.infoholdcity.common.mapper.SysUserMapper;
import com.infoholdcity.common.service.ISysUserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
@DS("slave_1")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public Result checkUserIsEffective(SysUser sysUser) {
        Result<?> result = new Result<Object>();
        //情况1：根据用户信息查询，该用户不存在
        if (sysUser == null) {
            result.error500("该用户不存在，请注册");
            return result;
        }
        //情况2：根据用户信息查询，该用户已注销
        if (CommonConstant.DEL_FLAG_1.equals(sysUser.getDelFlag())) {
            result.error500("该用户已注销");
            return result;
        }
        //情况3：根据用户信息查询，该用户已冻结
        if (CommonConstant.USER_FREEZE.equals(sysUser.getStatus())) {
            result.error500("该用户已冻结");
            return result;
        }
        return result;
    }

    @Override
    @CacheEvict(value= {CacheConstant.SYS_USERS_CACHE}, key="#username")
    public void updateUserDepart(String username,String orgCode) {
        baseMapper.updateUserDepart(username, orgCode);
    }
}
